package com.raycloud.java.dada.service.impl;

import com.alibaba.fastjson.JSON;
import com.raycloud.java.dada.base.BaseService;
import com.raycloud.java.dada.base.Config;
import com.raycloud.java.dada.model.AppendDeliver;
import com.raycloud.java.dada.model.OrderStatus;
import com.raycloud.java.dada.model.ShopInfo;
import com.raycloud.java.dada.result.*;
import com.raycloud.java.dada.service.inter.InformationService;
import com.raycloud.java.dada.util.BasicURL;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by:  intelliJ IDEA
 *
 * @Author albert
 * @Description:各种查询操作接口实现
 * @Date:2017/6/23 下午7:17
 * @package:com.raycloud.java.dada.service.impl
 * @Modified By:
 */
public class InformationServiceImpl extends BaseService implements InformationService {

    public InformationServiceImpl(Config config) {
        super(config);
    }

    @Override
    public List<OrderCancelReasonResult> selectOrderCancelReason() throws Exception {

        String result = doPost(null, BasicURL.ORDER_FORMAL_CANCEL_REASON);
        ResultModel model = strToResultModel(result);
        List<OrderCancelReasonResult> reasonResults = new ArrayList<OrderCancelReasonResult>();
        if ("fail".equals(model.getStatus())) {
            //调用接口失败 返回错误信息
            throw new Exception("接口调用失败，错误代码" + model.getCode() + "错误信息" + model.getMsg());
        } else {
            reasonResults = JSON.parseArray(model.getResult(), OrderCancelReasonResult.class);
        }

        return reasonResults;
    }


    @Override
    public List<AppendDeliverResult> selectOrderTransporter(AppendDeliver appendDeliver) throws Exception {
        String result = doPost(appendDeliver, BasicURL.ORDER_APPOINT_LIST);
        ResultModel model = strToResultModel(result);
        List<AppendDeliverResult> appendDeliverResults = new ArrayList<AppendDeliverResult>();
        if ("fail".equals(model.getStatus())) {
            //调用接口失败 返回错误信息
            throw new Exception("接口调用失败，错误代码:" + model.getCode() + "错误信息:" + model.getMsg());
        } else {
            appendDeliverResults = JSON.parseArray(model.getResult(), AppendDeliverResult.class);
        }

        return appendDeliverResults;
    }

    @Override
    public OrderStatusResult selectOrderStatus(OrderStatus orderStatus) throws Exception {
        String result = doPost(orderStatus, BasicURL.ORDER_STATUS);
        ResultModel model = strToResultModel(result);
        OrderStatusResult statusResult = new OrderStatusResult();
        if ("fail".equals(model.getStatus())) {
            //调用接口失败 返回错误信息
            throw new Exception("接口调用失败，错误代码:" + model.getCode() + "错误信息:" + model.getMsg());
        } else {
            statusResult = JSON.parseObject(model.getResult(), OrderStatusResult.class);
        }
        return statusResult;
    }

    @Override
    public List<CityInfoResult> selectOrderCity() throws Exception {
        String result = doPost(null, BasicURL.CITY_CODE_LIST);
        ResultModel model = strToResultModel(result);
        List<CityInfoResult> cityInfoResults = new ArrayList<CityInfoResult>();
        if ("fail".equals(model.getStatus())) {
            //调用接口失败 返回错误信息
            throw new Exception("接口调用失败，错误代码:" + model.getCode() + "错误信息:" + model.getMsg());
        } else {
            cityInfoResults = JSON.parseArray(model.getResult(), CityInfoResult.class);
        }

        return cityInfoResults;
    }

    @Override
    public ShopResult selectShopInfo(ShopInfo shopInfo) throws Exception {
        String result = doPost(shopInfo, BasicURL.SHOP_DETAIL);
        ResultModel model = strToResultModel(result);
        ShopResult shopResult = null;
        if ("fail".equals(model.getStatus())) {
            //调用接口失败 返回错误信息
            throw new Exception("接口调用失败，错误代码:" + model.getCode() + "错误信息:" + model.getMsg());
        } else {
            shopResult = JSON.parseObject(model.getResult(), ShopResult.class);
        }
        return shopResult;
    }

    @Override
    public List<ComplainReasonResult> selectComplaintDadaReason() throws Exception {
        String result = doPost(null, BasicURL.COMPLAINT_DADA_REASON);
        ResultModel model = strToResultModel(result);
        List<ComplainReasonResult> complainReasonResults = new ArrayList<ComplainReasonResult>();
        if ("fail".equals(model.getStatus())) {
            //调用接口失败 返回错误信息
            throw new Exception("接口调用失败，错误代码:" + model.getCode() + "错误信息:" + model.getMsg());
        } else {
            complainReasonResults = JSON.parseArray(model.getResult(), ComplainReasonResult.class);
        }

        return complainReasonResults;
    }
}
